import api from '../../lib/httpapi'
import db from '../../models/adm'

const start = {
    namespaced: true,
    state: {
        items: [],
        conn: {
            url: 'http://10.13.2.187:9080',
            status: null
        },
        isVerify: false
    },
    getters: {
        hasDefaultAdmin: state => {
        },
        all: state => {
            let data = state.items.filter((val) => (!!val.username && !!val.password) ? true : false).map((val, i) => {
                val.serialno = i + 1
                return val
            })
            return data
        },
        admin_counts: (state)=>state.items.length
    },
    mutations: {
        insertItems(state, data) {
            try {
                state.items = data
                return true
            } catch (e) {
                return false
            }
        },
        add(state, data) {
            state.items.push(data)
        },
        del(state, data) {
            state.items = state.items.filter((v, i) => {
                if (v.username == data.username && v.password == data.password)
                    return false
                else
                    return true
            })
        },
        update(state, { obj, uval }) {
            state.items = state.items.map((v) => {
                if (v.username == obj.username && v.password == obj.password) {
                    v.username = uval.username
                    v.password = uval.password
                }
                return v
            })
        },
        canPing(state, data) {
            for (var i = 0; i < state.conns.length; i++)
                if (state.conns[i].url == data.url) state.conns[i].status = data.status
        },

        ping(state, data) {
            state.conn.status = data.status
        },

        // 登陆验证
        verify(state, account) {
            for (var a of state.items) {
                if (a.username == account.username && a.password == account.password){
                    state.isVerify = true
                    return
                }
            }
            state.isVerify = false
        },

        logout(state){
            state.isVerify = false
        }
    },
    actions: {
        async init({ commit }) {
            try {
                let rsl = await db.find({})
                if (!!rsl && rsl.length != 0)
                    commit('insertItems', rsl)
            } catch (e) {
                console.log(e)
            }
        },

        async insert({ commit }, obj) {
            try {
                try{
                    await db.findOne(obj)
                }catch(err){
                    await db.insert(obj)
                    commit('add', obj)
                }
            } catch (e) {
                console.log(e)
            }
        },

        async remove({ commit }, obj) {
            try {
                if (obj.serialno)
                    delete obj.serialno
                const test = await db.remove(obj, { multi: false })
                commit("del", obj)
            } catch (e) {
                console.log(e);
            }
        },

        async modify({ commit }, { obj, uval }) {
            try {
                await db.update(obj, uval, { multi: false })
                commit("update", { obj, uval })
            } catch (e) {
                console.log(e)
            }
        },

        // async ping({ state, commit }) {
        //     for (var item of state.conns) {
        //         try {
        //             const res = await api.ping(item.url, { can: false })
        //             console.log(res)
        //             if (res.status == 200)
        //                 commit('canPing', { key: item.url, status: true })
        //             else
        //                 commit('canPing', { key: item.url, status: false })
        //         } catch (err) {
        //             console.log(err)
        //             commit('canPing', { key: item.url, status: false })
        //         }
        //     }
        // }

        // async ping() {
        //     try {
        //         const res = await $socket.io.emit('ping', JSON.stringify({ can: false }))
        //         console.log(res)
        //     } catch (err) {
        //         console.log(err)
        //     }
        // }
    }
}

export default start